package game.assets;

import haxe.io.Path;
import hx.assets.Assets;
import game.data.ProjectRoleData;
import hx.assets.StringFuture;
import hx.assets.Future;

/**
 * 加载.abproj文件的角色数据
 */
class ABProjFuture extends Future<RoleAssets, String> {
	public function new(path:String) {
		super(path, false);
	}

	override function post() {
		super.post();
		// 先加载.abproj文件
		var parentDir = Path.directory(path);
		new StringFuture(path).onComplete((data) -> {
			// 加载完成后，需要解析为IRoleData数据
			var roleData = new ProjectRoleData(data);
			// 需要解析数据，分析所需要的图片、音频资源加载
			var assets = new RoleAssets();
			roleData.parseLoadFilesToAssets(assets, parentDir);
			assets.onComplete((a) -> {
				assets.id = Path.withoutExtension(Path.withoutDirectory(path));
				assets.roleDatas.set(assets.id, roleData);
				assets.data = roleData;
				this.completeValue(assets);
			}).onError(this.errorValue);
			assets.start();
		}).onError(this.errorValue);
	}
}
